class Connection
   attr_accessor :time
   include Comparable
    def <=>(anOther)
       self.to_s <=> anOther.to_s
    end

    def initialize(time, color)
      @time          =  time
      @timeout       =  false

      @color         =  color
      @tmp_color     =  @color

      @fading        =  SECONDS_TO_FADEOUT
      @fading_steps  =  calc_steps
    end

    def timeout?
        @timeout
    end

    # TO OVERRIDE BY CHILD
    def display(bases)
       # filter out not available fields
      
       bases = bases.find_all{|x| 
           begin
              self.send(x)
              true
           rescue NoMethodError => e
               false
           end
       }

       if bases.size >= 2
           process()

           if !timeout?
               (bases.size-1).times do |base_index|
#                   pp [self.class, "drawing from #{bases[base_index]} to #{bases[base_index+1]}"]
                   drawLine( PLOT.get_base( bases[base_index]   ).x,   
                             PLOT.get_base( bases[base_index]   ).value_to_pos( self.send( bases[base_index])), 
                             PLOT.get_base( bases[base_index+1] ).x,   
                             PLOT.get_base( bases[base_index+1] ).value_to_pos( self.send( bases[base_index+1] ))
                            )
=begin
                   if base_index == 0 
                       glRasterPos2f(PLOT.get_base( bases[base_index]   ).x-0.25, 
                                     PLOT.get_base( bases[base_index]   ).value_to_pos( self.send( bases[base_index] )) 
                                    )
                       self.send( bases[base_index] ).to_s.each_byte{|x| GLUT.BitmapCharacter(0, x) }
                   elsif base_index == (bases.size-2)
                       glRasterPos2f(PLOT.get_base( bases[base_index+1]   ).x+0.02, 
                                     PLOT.get_base( bases[base_index+1]   ).value_to_pos( self.send( bases[base_index] )) 
                                    )
                       self.send( bases[base_index+1] ).to_s.each_byte{|x| GLUT.BitmapCharacter(0, x) }
                   end
=end
               end
           end
       end
    end

    def update(time)
        @time        = time
        reset_color
    end

    def method_missing(*args)
        @pkt.send(*args)
    end

private
    def reset_color
        @tmp_color   = @color 
    end
    def calc_step_color(color, step)
        slice = (1.0 / @fading_steps.size)
        color_value = ((@fading_steps.size) - step)*slice

        color.collect{|x| x*color_value}
    end

    def calc_steps
        step = (@fading / FADING_STEPS.to_f)
	result = []
        FADING_STEPS.times do |index|
		result.push( step*(index+1) )
	end
	return result
    end

    def process
       display_time = Time.now
       
       diff = (display_time - @time)

       step = 0

       @fading_steps.each_with_index{|tmp_step, index|
          if diff < tmp_step
              step = index+1
              break
          elsif @fading_steps[index-1] && diff >= @fading_steps[index-1] && diff < tmp_step
              step = index+1
          end
       }

       if step == 0
           @timeout = true 
       else
           @tmp_color = calc_step_color(@color, step) 
       end
    end

    def ip_to_pixel(ip, filter=[0, 2.power!(32)])
      filter[0]+((1.0/filter[1])*ip.to_i)
    end

    def port_to_pixel(port, filter=[0, 2.power!(16)])
      filter[0]+(( 1.0 / filter[1] )*port.to_i)
    end

    def drawLine(x_start, y_start, x_end, y_end)
        if @tmp_color[3]
          glColor(@tmp_color[0], @tmp_color[1], @tmp_color[2], @tmp_color[3])
        else
          glColor(@tmp_color[0], @tmp_color[1], @tmp_color[2])
        end
        glBegin(GL_LINE_STRIP);
            glVertex(x_start+0.005, y_start);
            glVertex(x_end+0.005,   y_end);
        glEnd();
    #    glBegin(GL_LINE_STRIP);
    end
end
